import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.io.*;
import java.rmi.*;
import java.net.*;

public class MCQSetting extends JInternalFrame implements InterInterface,InternalFrameListener
{
		public JTextArea txarea = new JTextArea(500,500);
		public static int t = 1;
		public MCQSetting()
	         {
		super("MCQ Question Setting",false,true,false,false);
		this.setBounds(30,10,500,400);
		setVisible(true);
		//txarea.setText("");
		Container c = getContentPane();
		c.setLayout(null);
		lab28.setText("                              MCQ Question Setting");
		lab28.setFont(new Font("Verdana",Font.BOLD,16));
		lab28.setEditable(false);
		lab28.setForeground(Color.black);
		lab28.setBackground(Color.gray);
		lab28.setBounds(0,0,500,30);
		c.add(lab28);
		
		bt29.setEnabled(true);
		lab33.setVisible(true);
		
		lab30.setBounds(70,30,180,25);
		lab30.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab30);
		lab31.setBounds(250,30,50,25);
		lab31.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab31);
		lab31.setText(txa02.getText());
		
		lab32.setBounds(50,60,150,25);
		lab32.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(lab32);
		lab33.setBounds(150,60,50,25);
		lab33.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(lab33);
		
		s03.setBounds(50,90,400,60);
		c.add(s03);

		lab34.setBounds(50,160,10,25);
		lab34.setFont(new Font("Verdana",Font.PLAIN,13));
		txf34.setBounds(65,160,350,25);
		lab35.setBounds(50,185,10,25);
		lab35.setFont(new Font("Verdana",Font.PLAIN,13));
		txf35.setBounds(65,185,350,25);
		lab36.setBounds(50,210,10,25);
		lab36.setFont(new Font("Verdana",Font.PLAIN,13));
		txf36.setBounds(65,210,350,25);
		lab37.setBounds(50,235,10,25);
		lab37.setFont(new Font("Verdana",Font.PLAIN,13));
		txf37.setBounds(65,235,350,25);
		c.add(lab34);
		c.add(lab35);
		c.add(lab36);
		c.add(lab37);
		
		c.add(txf34);
		c.add(txf35);
		c.add(txf36);
		c.add(txf37);
		
		lab38.setBounds(50,270,130,25);
		lab38.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(lab38);
		txf38.setBounds(180,273,50,22);
		txf38.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(txf38);
		
		lab39.setBounds(50,300,130,25);
		lab39.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(lab39);
		txf39.setBounds(180,303,100,22);
		txf39.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(txf39);
		txf39.setEnabled(false);
		txf39.setText("");
		bt30.setBounds(90,330,130,38);
		bt30.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(bt30);
		bt30.setEnabled(false);
		bt31.setBounds(240,330,130,38);
		bt31.setFont(new Font("Verdana",Font.PLAIN,13));
		c.add(bt31);
		bt31.setEnabled(false);
		
		bt29.setBounds(310,267,110,38);
		bt29.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(bt29);
		
		lab33.setText("1");
		
		bt29.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			SaveQuestion();
		}
		}
		);
		
		bt30.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			try {
				Integer.parseInt(txf39.getText());
				try {
					MainServerImpl msi = new MainServerImpl();
					Naming.rebind("MainServer",msi);
					JOptionPane.showMessageDialog(null,"Exam Start Successfully..","Thanks",JOptionPane.INFORMATION_MESSAGE);
					System.out.println("Waiting For Clients");
				}
				catch(Exception ext) {
					JOptionPane.showMessageDialog(null,"Server Internal Error..\n Contact to the Administrator..","Error",JOptionPane.ERROR_MESSAGE);
				}
				
				Theread thr1 = new Theread("Thread1");
				thr1.start();
			}
			catch(Exception ext) {
				JOptionPane.showMessageDialog(null,"Exam cannot Start...","Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		}
		);
		
		bt31.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			MCQResult vl = new MCQResult();
			vl.MCQResult1();	
			desktop.add(vl);
				try{
				vl.setSelected(true);}
				catch(java.beans.PropertyVetoException p){};
		}
		}
		);
		
		try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		
	         }
	
	}
	 
	 private void SaveQuestion()
	 {
	 	if(((txa03.getText()).equals("")) || ((txf34.getText()).equals("")) || ((txf35.getText()).equals("")) || ((txf36.getText()).equals("")) || ((txf37.getText()).equals("")) || (txf38.getText()).equals(""))
	 	JOptionPane.showMessageDialog(null,"Error Building Question !!!","Error",JOptionPane.ERROR_MESSAGE);
	 	else {
	 		if(txf38.getText().equals("a") || txf38.getText().equals("b") || txf38.getText().equals("c") || txf38.getText().equals("d") || txf38.getText().equals("A") || txf38.getText().equals("B") || txf38.getText().equals("C") || txf38.getText().equals("D"))
	 		SavingQuestion();
	 		//JOptionPane.showMessageDialog(null,"Correct Building","Correct",JOptionPane.PLAIN_MESSAGE);
	 		else {
	 			JOptionPane.showMessageDialog(null,"Choose correct Ans. A,B,C or D","Error",JOptionPane.INFORMATION_MESSAGE);
	 		}
	 	}
	}
	
	private void SavingQuestion() {
		txarea.append(txa03.getText()+"\n");
		txarea.append(txf34.getText()+"\n");
		txarea.append(txf35.getText()+"\n");
		txarea.append(txf36.getText()+"\n");
		txarea.append(txf37.getText()+"\n");
		txarea.append(txf38.getText()+"\n");
		txa03.setText("");
		txf34.setText("");
		txf35.setText("");
		txf36.setText("");
		txf37.setText("");
		txf38.setText("");
		
		if(t == Integer.parseInt(lab31.getText())) {
		JOptionPane.showMessageDialog(null,"All Questions editing complete..","Thanks",JOptionPane.INFORMATION_MESSAGE);
		saveToFile();
		bt29.setEnabled(false);
		lab33.setVisible(false);
		txf39.setEnabled(true);
		bt30.setEnabled(true);
		//bt31.setEnabled(true);
		}
		t++;
		lab33.setText(Integer.toString(t));
	}
	 private void saveToFile() {
	 	
	 	File fina = new File("mcq_ques/"+txafile.getText());
		try
			{
				FileWriter out = new FileWriter(fina);
				String text = txarea.getText();
				
				out.write(text);
				//p = 1;
				out.close();
			}
			catch(IOException e)
			{
				JOptionPane.showMessageDialog(this,"Error Writting the File","Error",JOptionPane.ERROR_MESSAGE);
			}
	}
	
	
	 
	 public void internalFrameClosing(InternalFrameEvent e) {
    	
   }

    public void internalFrameClosed(InternalFrameEvent e) {
       
      }
	 public void internalFrameOpened(InternalFrameEvent e) {
    
            
    }

    public void internalFrameIconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameDeiconified(InternalFrameEvent e) {
    	
        
    }

    public void internalFrameActivated(InternalFrameEvent e) {
    	
         
    }

    public void internalFrameDeactivated(InternalFrameEvent e) {
         
    }	
}
